// pages/myReward/myReward.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabData:[
      { txt: '我打赏', current: true },
      { txt: '被打赏', current: false }
    ],
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData0: true,
    hasData1: true,
    actiItem0: [],
    actiItem1: [],
    page0: 1,
    page1: 1,
    loadSuccess:false,
    userInfo:'',
    current:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      loadSuccess: false,
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
    })
    wx.showLoading();
    this.getReword(true);
    this.getAwarded(true);
  },
  getReword:function(flag){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyReward/page',
      data: {
        currentPage: this.data.page0,
        pageNumber: '20',
        fromUserId: this.data.userInfo.id,
        status: 1
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            _this.setData({
              actiItem0: []
            });
          } else {
            _this.setData({
              loadMore: false
            });
          }
          if (res.data.data.pp) {
            if (res.data.data.pp.length < 20) {
              _this.setData({
                hasData0: false
              })
            } else {
              _this.setData({
                hasData0: true
              })
            }
            _this.data.actiItem0 = _this.data.actiItem0.concat(res.data.data.pp);
            _this.setData({
              actiItem0: _this.data.actiItem0
            })
          }
        }
      }
    })
  },
  getAwarded: function (flag){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyReward/page',
      data: {
        currentPage: this.data.page1,
        pageNumber: '20',
        toUserId: this.data.userInfo.id,
        status: 1
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            _this.setData({
              actiItem1: []
            });
          } else {
            _this.setData({
              loadMore: false
            });
          }
          if (res.data.data.pp) {
            if (res.data.data.pp.length < 20) {
              _this.setData({
                hasData1: false
              })
            } else {
              _this.setData({
                hasData1: true
              })
            }
            _this.data.actiItem1 = _this.data.actiItem1.concat(res.data.data.pp);
            _this.setData({
              actiItem1: _this.data.actiItem1
            })
          }
        }
      }
    })

  },
  swichTab: function (e) {
    this.data.tabData.forEach(function (ele) {
      ele.current = false;
    })
    this.data.tabData[e.currentTarget.dataset.value].current = true;
    this.setData({
      tabData: this.data.tabData,
      current: parseInt(e.currentTarget.dataset.value)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      ['page' + this.data.current]: 1
    })
    if (this.data.current == 0) {
      this.getReword(true);
    } else {
      this.getAwarded(true);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data['hasData' + this.data.current])
    if (this.data['hasData' + this.data.current]) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        ['page' + this.data.current]: this.data['page' + this.data.current] + 1
      });
      if (this.data.current == 0) {
        this.getReword();
      } else {
        this.getAwarded();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})